package com.mobile.liuyi_s.orderlunch.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.mobile.liuyi_s.orderlunch.BaseActivity;
import com.mobile.liuyi_s.orderlunch.MainActivity;
import com.mobile.liuyi_s.orderlunch.R;
import com.mobile.liuyi_s.orderlunch.entity.MyUser;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

public class ChangePwdActivity extends BaseActivity implements View.OnClickListener {

    //    private MyApplication mApplication;
    private String mInputOriginalPwd;
    private String mInputNewPwd;
    private String mInputNewPwdConfirm;
    private static final String TAG = "ChangePwdActivity";

    private EditText mEditTextOriginalPwd;
    private EditText mEditTextNewPwd;
    private EditText mEditTextNewPwdConfirm;
    private Button mButtonCommit;
    private Button mButtonCancel;
    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);
        initView();
        String username = MyUser.getCurrentUser().getUsername();
        String tableName = MyUser.getCurrentUser().getTableName();
        Log.d(TAG, "onCreate:  " + username + "  " + tableName);
    }

    private void initView() {
        initToolbar();
        mEditTextOriginalPwd = (EditText) findViewById(R.id.edt_original_change_pwd_activity);
        mEditTextNewPwd = (EditText) findViewById(R.id.edt_new_pwd_change_pwd_activity);
        mEditTextNewPwdConfirm = ((EditText) findViewById(R.id.edt_new_pwd_confirm_change_pwd_activity));
        mButtonCommit = (Button) findViewById(R.id.btn_change_pwd_confirm);
        mButtonCancel = ((Button) findViewById(R.id.btn_change_pwd_cancel));
        mButtonCommit.setOnClickListener(this);
        mButtonCancel.setOnClickListener(this);
    }

    private void initToolbar() {
        mToolbar = ((Toolbar) findViewById(R.id.toolbar_change_pwd));
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
    }

    @Override
    public void onClick(View view) {
        mInputOriginalPwd = mEditTextOriginalPwd.getText().toString();
        mInputNewPwd = mEditTextNewPwd.getText().toString().trim();
        mInputNewPwdConfirm = mEditTextNewPwdConfirm.getText().toString().trim();
        switch (view.getId()) {
            case R.id.btn_change_pwd_confirm:
                Log.d(TAG, "onClick: originalPwd：" + mInputOriginalPwd + " NewPwd：" + mEditTextNewPwdConfirm);
                if (mInputOriginalPwd.length() >= 3) {
                    if (mInputNewPwd.length() >= 3 && mInputNewPwdConfirm.length() >= 3) {
                        if (mInputNewPwd.equals(mInputNewPwdConfirm)) {
                            updateCurrentUserPassword(mInputOriginalPwd, mInputNewPwd);
                        } else {
                            Toast.makeText(this, "两次新密码输入不一致", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(this, "新密码最少三位", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "原密码长度最少三位", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_change_pwd_cancel:
                finish();
                break;
            default:
                break;
        }
    }

    private void updateCurrentUserPassword(String originalPwd, String newPwd) {
        addSubscription(BmobUser.updateCurrentUserPassword(originalPwd, newPwd, new UpdateListener() {
            @Override
            public void done(BmobException e) {
                Log.d(TAG, "done: " + e);
                if (e == null) {
                    showDialog();
                } else if (e.toString().contains("210")) {
                    Toast.makeText(ChangePwdActivity.this, "原密码错误！", Toast.LENGTH_SHORT).show();
                } else if (e.toString().contains("206")) {
                    Toast.makeText(ChangePwdActivity.this, "服务器故障！", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(ChangePwdActivity.this, "修改失败！", Toast.LENGTH_SHORT).show();
                }
            }
        }));

    }

    private void showDialog() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("提示");
        dialog.setMessage("密码修改成功，即将退出！");
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                MyUser.logOut();
                Intent intent = new Intent(ChangePwdActivity.this, MainActivity.class);
                intent.putExtra("finish", true);
                startActivity(intent);
            }
        });
        dialog.show();
    }


}
